package com.hzhk.happyrent.ui;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.CodeModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyResponseCallback;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.activity.order.OrderDetailActivity;
import com.hzhk.happyrent.ui.activity.order.OrderListActivity;
import com.hzhk.happyrent.utils.MyPerference;
import com.tamic.novate.Throwable;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/1/17 0017.
 */

public class OrderCancelDialog {
    @BindView(R.id.cancel_close)
    ImageView cancelClose;
    @BindView(R.id.cancel_reason1)
    LinearLayout cancelReason1;
    @BindView(R.id.cancel_reason2)
    LinearLayout cancelReason2;
    @BindView(R.id.cancel_reason3)
    LinearLayout cancelReason3;
    @BindView(R.id.cancel_reason4)
    LinearLayout cancelReason4;
    @BindView(R.id.cancel_reason5)
    LinearLayout cancelReason5;
    @BindView(R.id.cancel_reason6)
    LinearLayout cancelReason6;
    @BindView(R.id.cancel_reason7)
    LinearLayout cancelReason7;
    @BindView(R.id.order_submit)
    Button orderPay;
    private Context context;
    private View popView;
    private Dialog dialog;
    private String message="";
    private String order_Id;
    public OrderCancelDialog(Context context,String order_Id) {
        this.context = context;
        this.order_Id=order_Id;
        initDialog();
    }

    private void initDialog() {
        dialog = new Dialog(context, R.style.CancelDialog);
        popView = ((Activity) context).getLayoutInflater().inflate(R.layout.view_order_cancel, null);
        ButterKnife.bind(this, popView);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        //获取屏幕宽
        int weight = context.getResources().getDisplayMetrics().widthPixels;
        dialog.setContentView(popView);
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(weight, FrameLayout.LayoutParams.WRAP_CONTENT);
        popView.setLayoutParams(params);
        Window dialogWindow = dialog.getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.BOTTOM;
        dialogWindow.setAttributes(lp);
        dialog.show();
    }

    public boolean isShowing() {
        if (dialog != null && dialog.isShowing()) {
            return true;
        } else {
            return false;
        }
    }

    public void closeCancel() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @OnClick({R.id.cancel_close, R.id.cancel_reason1, R.id.cancel_reason2, R.id.cancel_reason3, R.id.cancel_reason4, R.id.cancel_reason5, R.id.cancel_reason6, R.id.cancel_reason7, R.id.order_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cancel_close:
                closeCancel();
                break;
            case R.id.cancel_reason1:
                checkIt(cancelReason1);
                break;
            case R.id.cancel_reason2:
                checkIt(cancelReason2);
                break;
            case R.id.cancel_reason3:
                checkIt(cancelReason3);
                break;
            case R.id.cancel_reason4:
                checkIt(cancelReason4);
                break;
            case R.id.cancel_reason5:
                checkIt(cancelReason5);
                break;
            case R.id.cancel_reason6:
                checkIt(cancelReason6);
                break;
            case R.id.cancel_reason7:
                checkIt(cancelReason7);
                break;
            case R.id.order_submit:
                initData();
                break;
        }
    }

    private void initData() {
        if(TextUtils.isEmpty(message)){
            Toast.makeText(context, "请选择原因", Toast.LENGTH_SHORT).show();
            return;
        }
        Map<String,Object> params=new HashMap<>();
        params.put("user_id", MyPerference.getInstance().getUserId());
        params.put("order_id", order_Id);
        params.put("cancel_reasion", message.substring(2,message.length()));
        HttpManager.getInstance().perform_post(API.ORDER_CANCEL, params, new MyStringResponse( context) {
            @Override
            public void onNext(Object tag, String response) {
                CodeModel codeModel=new Gson().fromJson(response,CodeModel.class);
                if(codeModel.getErrCode()==0){
                    Toast.makeText(context, "取消成功", Toast.LENGTH_SHORT).show();
                    closeCancel();
                    if(context instanceof OrderDetailActivity){
                        ((Activity) context).finish();
                    }else if(context instanceof OrderListActivity){
                        ((OrderListActivity) context).onRefresh();
                    }
                }else{
                    closeCancel();
                    Toast.makeText(context, codeModel.getMessage(), Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });
    }

    private void checkIt(LinearLayout cancelReason) {
        cancelReason1.getChildAt(0).setSelected(false);
        cancelReason2.getChildAt(0).setSelected(false);
        cancelReason3.getChildAt(0).setSelected(false);
        cancelReason4.getChildAt(0).setSelected(false);
        cancelReason5.getChildAt(0).setSelected(false);
        cancelReason6.getChildAt(0).setSelected(false);
        cancelReason7.getChildAt(0).setSelected(false);
        cancelReason.getChildAt(0).setSelected(true);
        message=((TextView)(cancelReason.getChildAt(1))).getText().toString();
    }
}
